package edu.gatech.cs2340.OregonTrail;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JOptionPane;

import edu.gatech.cs2340.OregonTrail.EventSystem.ActorEvent;
import edu.gatech.cs2340.OregonTrail.EventSystem.ActorListener;
import edu.gatech.cs2340.OregonTrail.EventSystem.IllnessEvent;


/**
 * A representation of a party on on the Oregon Trail. Responsible for holding party members  as well as
 * the player and affecting any changes made to their status
 * @author Won Taek Chung
 * @version 1.0.1
 */
public class Party implements ActorListener{
	private Player player; 
	private ArrayList<PartyMember> party;
	
	/**
	 * Default constructor for party class
	 * @param player Player object to use as the party's player
	 * @param pm1 PartyMember object to use as party member 1
	 * @param pm2 PartyMember object to use as party member 2
	 * @param pm3 PartyMember object to use as party member 3
	 * @param pm4 PartyMember object to use as party member 4
	 */
	public Party(Player player,PartyMember pm1, PartyMember pm2, PartyMember pm3, PartyMember pm4)
	{
		party = new ArrayList<PartyMember>();
		this.player = player;
		this.party.add(pm1);
		this.party.add(pm2);
		this.party.add(pm3);
		this.party.add(pm4);
	}

	/**
	 * Getter for the Player instance variable
	 * @return The party's player
	 */
	public Player getPlayer() {
		return player;
	}

	/**
	 * Setter for the Player instance variable (Note: use primarily for testing. Players should not be
	 * swapped in normal game flow)
	 * @param player New player object to be used as the player
	 */
	public void setPlayer(Player player) {
		this.player = player;
	}

	/**
	 * Getter for the party instance variable
	 * @return ArrayList containing all the party members other than the player
	 */
	public ArrayList<PartyMember> getParty() {
		return party;
	}

	/**
	 * Setter for party instance variable
	 * @param party New list of party members to use
	 */
	public void setParty(ArrayList<PartyMember> party) {
		this.party = party;
	}

	@Override
	public void actorActionPerformed(ActorEvent e) {
		if(e.getClass() == IllnessEvent.class){
			Random rand = new Random();
			int member = rand.nextInt(5);
			//We should have put the player in the party array
			if(member == 4){
				if(player.getStatus() == eStatus.sick && ((IllnessEvent) e).getNewStatus() == eStatus.dead){
					player.setStatus(((IllnessEvent) e).getNewStatus());
					JOptionPane.showMessageDialog(null, player.getName() + ((IllnessEvent) e).getMessage());
				}
				else if(((IllnessEvent) e).getNewStatus() != eStatus.dead){
					player.setStatus(((IllnessEvent) e).getNewStatus());
					JOptionPane.showMessageDialog(null, player.getName() + ((IllnessEvent) e).getMessage());
				}
			}
			else{
				if(party.get(member).getStatus() == eStatus.sick && ((IllnessEvent) e).getNewStatus() == eStatus.dead){
					party.get(member).setStatus(((IllnessEvent) e).getNewStatus());
					JOptionPane.showMessageDialog(null, party.get(member).getName() + ((IllnessEvent) e).getMessage());
				}
				else if(((IllnessEvent) e).getNewStatus() != eStatus.dead){
					party.get(member).setStatus(((IllnessEvent) e).getNewStatus());
					JOptionPane.showMessageDialog(null, party.get(member).getName() + ((IllnessEvent) e).getMessage());
				}
			}
		}
		
	}
}
